package com.example.socketio.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import com.corundumstudio.socketio.SocketIOServer;
import com.example.socketio.handler.SocketIOHandler;

//配置SocketIO
@Configuration
public class SocketIOConfig {
    @Value("${socketio.host}")
    private String host;

    @Value("${socketio.port}")
    private Integer port;

    @Value("${socketio.bossCount}")
    private int bossCount;

    @Value("${socketio.workCount}")
    private int workCount;

    @Value("${socketio.allowCustomRequests}")
    private boolean allowCustomRequests;

    @Value("${socketio.upgradeTimeout}")
    private int upgradeTimeout;

    @Value("${socketio.pingTimeout}")
    private int pingTimeout;

    @Value("${socketio.pingInterval}")
    private int pingInterval;

    @Bean
    public SocketIOServer server() {
        //各种配置，地址，消息单次最大传输量，最大连接数等...
        //暂时可以忽略
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setHostname(host);
        config.setPort(port);
        config.setOrigin("http://localhost:5173");
        config.setBossThreads(bossCount);
        config.setWorkerThreads(workCount);
        config.setAllowCustomRequests(allowCustomRequests);
        config.setUpgradeTimeout(upgradeTimeout);
        config.setPingTimeout(pingTimeout);
        config.setPingInterval(pingInterval);
        

        //创建一个服务器实例并赋予上面的配置
        SocketIOServer server=new SocketIOServer(config);

        //让这个服务器可以监听到前端发送的connect，event，disconnect这些事件
        //SocketIOHandler是一个自定义的类，里面包括onConnect等方法，这些方法就是当前端连接建立时，发送消息时在服务端会触发的事件
        //不添加监听服务器就无法接收到客户端发送的事件
       // server.addListeners(new SocketIOHandler());

        return server;
    }
}
